package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;

import com.gxuwz.medical.dao.ApplyDao;
import com.gxuwz.medical.dao.MoneyDao;
import com.gxuwz.medical.domain.apply.Apply;
import com.gxuwz.medical.domain.chronicdis.Chronicdis;
import com.gxuwz.medical.domain.family.FamilyMember;
import com.gxuwz.medical.domain.money.Money;
import com.gxuwz.medical.exception.DbException;

public class ApplyServlet extends BaseServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String m = req.getParameter("m");// 请求处理动作类型:list:显示列表；get：根据ID读取记录;input:新增数据;add:保存新记录；edit：更新记录
		String url = ERROR_PATH;
		try {
			if ("list".equals(m)) {	
				url="/page/apply/apply_list.jsp";
			}else if ("find".equals(m)) {
				url="/page/apply/apply_find.jsp";
			} else if ("input".equals(m)) {
				url="/page/apply/apply_add.jsp";
			} else if ("get".equals(m)) {
				url="/page/apply/apply_edit.jsp";				
			}else if ("exportApply".equals(m)) {
				exportApply(req, resp);	
			} else if ("add".equals(m)) {
				url = this.add(req, resp);
			}
			   process(req, resp, url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		  

	}
	/**
	 * 新增报销信息
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	private String add(HttpServletRequest req,HttpServletResponse resp)throws Exception{
		
		System.out.println("进入add"); 
		
		 String membercode=req.getParameter("membercode");
		 String illcode=req.getParameter("illcode");
		 String allcost=req.getParameter("allcost");
		 String hosinvoice=req.getParameter("hosinvoice");
		 String seetime=req.getParameter("seetime");
		 String applymoney=req.getParameter("applymoney");
		 //String alreadgot=req.getParameter("alreadgot");
		 System.out.println("applymoney"+applymoney);
		 
		 //从session对象中获取当前用户名称
		 String creator=(String)req.getSession().getAttribute("userid");
		//2:实例化慢病证管理模块组件
		 Apply model=new Apply();
		 //3:将请求参数赋值给实例的成员属性
		 model.setMembercode(new FamilyMember(membercode));
		 model.setIllcode(new Chronicdis(illcode));
		 model.setAllcost(allcost);
		 model.setHosinvoice(hosinvoice);
		 model.setApplymoney(applymoney);
		 model.setSeetime(seetime);
		 model.setAlreadgot(applymoney);
		 model.setCreator(creator);//建档人
		 model.setApplytime(new Date());
		 model.addApply();
		 return "/page/apply/apply_list.jsp";
		
	}	
	
	/**
	 * excel导出
	 * 
	 * @param req
	 * @param resp
	 *            void
	 * @throws IOException
	 * @throws DbException
	 */
	@SuppressWarnings("unused")
	private void exportApply(HttpServletRequest request,
			HttpServletResponse response) throws IOException, DbException {
		try {
			String id= request.getParameter("id");
			ApplyDao dao = new ApplyDao();
			List<Apply> list = dao.findById(id);//根据发票号，查出同一批的参合登记人，并打印出

			if (null != list && list.size() > 0) {

				String fileName = "慢性病报销登记信息.xls";
				response.setHeader(
						"Content-disposition",
						"attachment;filename="
								+ new String(fileName.getBytes("gb2312"),
										"ISO8859-1")); // 设置文件头编码格式
				response.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");// 设置类型
				response.setHeader("Cache-Control", "no-cache");// 设置头
				response.setDateHeader("Expires", 0);// 设置日期头

				HSSFWorkbook book = new HSSFWorkbook();

				HSSFSheet sheet = book.createSheet();
				CellStyle cellStyle = book.createCellStyle();

				cellStyle.setDataFormat(book.createDataFormat().getFormat(
						"yyyy-MM-dd"));

				CellRangeAddress region = new CellRangeAddress(0, // first row
						0, // last row
						0, // first column
						8 // last column
				);

				sheet.addMergedRegion(region);

				HSSFRow Row = sheet.createRow(0);
				HSSFCell headCell = Row.createCell(0);
				headCell.setCellValue("慢性病报销登记信息");

				// 设置单元格格式居中
				cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				headCell.setCellStyle(cellStyle);

				// 添加表头行
				Row = sheet.createRow(1);
				// 添加表头内容
				headCell = Row.createCell(0);
				headCell.setCellValue("姓名");
				headCell.setCellStyle(cellStyle);

				headCell = Row.createCell(1);
				headCell.setCellValue("疾病名称");
				headCell.setCellStyle(cellStyle);

				headCell = Row.createCell(2);
				headCell.setCellValue("医疗总费用");
				headCell.setCellStyle(cellStyle);

				headCell = Row.createCell(3);
				headCell.setCellValue("医院发票号");
				headCell.setCellStyle(cellStyle);

				headCell = Row.createCell(4);
				headCell.setCellValue("就诊时间");
				headCell.setCellStyle(cellStyle);

				headCell = Row.createCell(5);
				headCell.setCellValue("总报销金额");
				headCell.setCellStyle(cellStyle);

				headCell = Row.createCell(6);
				headCell.setCellValue("报销时间");
				headCell.setCellStyle(cellStyle);

				headCell = Row.createCell(7);
				headCell.setCellValue("登记员");
				headCell.setCellStyle(cellStyle);

				
				for (int i = 0; i < list.size(); i++) {
					Apply account = list.get(i);
					Row row = sheet.createRow((int) i + 2);

					Cell cell1 = row.createCell(0);
					cell1.setCellValue(account.getMembercode().getName());

					Cell cell2 = row.createCell(1);
					cell2.setCellValue(account.getIllcode().getIllname());
					Cell cell3 = row.createCell(2);
					cell3.setCellValue(account.getAllcost());
					Cell cell4 = row.createCell(3);
					cell4.setCellValue(account.getHosinvoice());
					Cell cell5 = row.createCell(4);
					cell5.setCellValue(account.getSeetime());

					Cell cell6 = row.createCell(5);
					cell6.setCellValue(account.getApplymoney());
					cell6.setCellStyle(cellStyle);

					Cell cell7 = row.createCell(6);
					cell7.setCellStyle(cellStyle);
					cell7.setCellValue(account.getApplytime());

					Cell cell8 = row.createCell(7);
					cell8.setCellStyle(cellStyle);
					cell8.setCellValue(account.getCreator());
				
				}

				book.write(response.getOutputStream());

				response.getOutputStream().flush();
				response.getOutputStream().close();
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
	}


}
